<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="档案类别" prop="archiveType">
        <el-input
          v-model="queryParams.archiveType"
          placeholder="请输入档案类别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="全宗号" prop="fondsNumber">
        <el-input
          v-model="queryParams.fondsNumber"
          placeholder="请输入全宗号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年度" prop="year">
        <el-input
          v-model="queryParams.year"
          placeholder="请输入年度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="题名" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入题名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:catalog:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:catalog:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:catalog:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:catalog:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['system:catalog:import']"
        >导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="catalogList" @selection-change="handleSelectionChange" border stripe>
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="档案类别" align="center" prop="archiveType" width="120" show-overflow-tooltip />
      <el-table-column label="全宗号" align="center" prop="fondsNumber" width="90" />
      <el-table-column label="年度" align="center" prop="year" width="80" />
      <el-table-column label="保管期限" align="center" prop="retentionPeriod" width="100" />
      <el-table-column label="机构或问题" align="center" prop="mechanicalIssue" width="100" show-overflow-tooltip />
      <el-table-column label="盒号" align="center" prop="boxNumber" width="80" />
      <el-table-column label="件号" align="center" prop="fileNumber" width="80" />
      <el-table-column label="系统文档名称" align="center" prop="docName" width="80" />
      <el-table-column label="题名" align="center" prop="title" min-width="200" show-overflow-tooltip />
      <el-table-column label="文件编号" align="center" prop="documentNumber" width="120" show-overflow-tooltip />
      <el-table-column label="责任者" align="center" prop="responsiblePerson" width="120" show-overflow-tooltip />
      <el-table-column label="附件" align="center" prop="attachment" width="80" />
      <el-table-column label="成文时间" align="center" prop="documentDate" width="100" />
      <el-table-column label="页数" align="center" prop="pageCount" width="70" />
      <el-table-column label="分类号" align="center" prop="classificationNumber" width="100" />
      <el-table-column label="密级" align="center" prop="securityLevel" width="80" />
      <el-table-column label="主题词" align="center" prop="subjectTerms" width="120" show-overflow-tooltip />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['system:catalog:query']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:catalog:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:catalog:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改目录管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :disabled="disabled">
        <el-row>
          <el-col :span="12">
            <el-form-item label="档案类别" prop="archiveType">
              <el-input v-model="form.archiveType" placeholder="请输入档案类别" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="全宗号" prop="fondsNumber">
              <el-input v-model="form.fondsNumber" placeholder="请输入全宗号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="年度" prop="year">
              <el-input v-model="form.year" placeholder="请输入年度" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="保管期限" prop="retentionPeriod">
              <el-input v-model="form.retentionPeriod" placeholder="请输入保管期限" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="机构或问题" prop="mechanicalIssue">
              <el-input v-model="form.mechanicalIssue" placeholder="请输入机构或问题" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="盒号" prop="boxNumber">
              <el-input v-model="form.boxNumber" placeholder="请输入盒号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="件号" prop="fileNumber">
              <el-input v-model="form.fileNumber" placeholder="请输入件号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="文件编号" prop="documentNumber">
              <el-input v-model="form.documentNumber" placeholder="请输入文件编号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="系统文档名" prop="docName">
          <el-input v-model="form.docName"  placeholder="请输入系统文档名" />
        </el-form-item>
        <el-form-item label="题名" prop="title">
          <el-input v-model="form.title" type="textarea" placeholder="请输入题名" />
        </el-form-item>
        <el-form-item label="责任者" prop="responsiblePerson">
          <el-input v-model="form.responsiblePerson" placeholder="请输入责任者" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="附件" prop="attachment">
              <el-input v-model="form.attachment" placeholder="请输入附件" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="成文时间" prop="documentDate">
              <el-input v-model="form.documentDate" placeholder="请输入成文时间" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="页数" prop="pageCount">
              <el-input v-model="form.pageCount" placeholder="请输入页数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分类号" prop="classificationNumber">
              <el-input v-model="form.classificationNumber" placeholder="请输入分类号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="密级" prop="securityLevel">
              <el-input v-model="form.securityLevel" placeholder="请输入密级" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主题词" prop="subjectTerms">
              <el-input v-model="form.subjectTerms" placeholder="请输入主题词" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="!disabled" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">{{ disabled ? '关 闭' : '取 消' }}</el-button>
      </div>
    </el-dialog>

    <!-- 目录导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的目录数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCatalog, getCatalog, delCatalog, addCatalog, updateCatalog } from "@/api/system/catalog";
import { getToken } from "@/utils/auth";

export default {
  name: "Catalog",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 目录管理表格数据
      catalogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否禁用表单
      disabled: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        archiveType: null,
        fondsNumber: null,
        year: null,
        title: null,
        documentNumber: null,
        responsiblePerson: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "题名不能为空", trigger: "blur" }
        ]
      },
      // 目录导入参数
      upload: {
        // 是否显示弹出层（目录导入）
        open: false,
        // 弹出层标题（目录导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的目录数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/catalog/importData"
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询目录管理列表 */
    getList() {
      this.loading = true;
      listCatalog(this.queryParams).then(response => {
        this.catalogList = response.rows;
        this.catalogList.forEach(item => {
          item.docName = this.genDocName(item)
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    //拼接字段
    genDocName(row){
      let name = '';
      if("振兴局"===row.dept){
        if(row.year.includes("WS")){
          let yearStr = row.year;
          name = row.fondsNumber+"-"+yearStr+"-"+row.retentionPeriod+"-"+row.mechanicalIssue+"-"+row.fileNumber
        }else {
          name = row.fondsNumber+"-"+row.retentionPeriod+"-"+row.year+"-"+row.mechanicalIssue+"-"+row.fileNumber
        }

      }else if("水利局" ===row.dept) {
        let yearStr = row.year.replace("WS", "WS·");
        let retentionPeriodStr;
        if('1'===row.retentionPeriod){
          retentionPeriodStr='Y'
        }else if('2'===row.retentionPeriod){
          retentionPeriodStr='D30'
        }else if('3'===row.retentionPeriod){
          retentionPeriodStr='D10'
        }
        name = row.fondsNumber+"-"+yearStr+"-"+retentionPeriodStr+"-"+row.fileNumber
      }
      return name;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        catalogId: null,
        archiveType: null,
        fondsNumber: null,
        year: null,
        retentionPeriod: null,
        mechanicalIssue: null,
        boxNumber: null,
        fileNumber: null,
        title: null,
        documentNumber: null,
        responsiblePerson: null,
        attachment: null,
        documentDate: null,
        pageCount: null,
        classificationNumber: null,
        securityLevel: null,
        subjectTerms: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.catalogId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.disabled = false;
      this.open = true;
      this.title = "添加目录管理";
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      this.disabled = true;
      const catalogId = row.catalogId || this.ids
      getCatalog(catalogId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看目录管理";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.disabled = false;
      const catalogId = row.catalogId || this.ids
      getCatalog(catalogId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改目录管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.catalogId != null) {
            updateCatalog(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCatalog(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const catalogIds = row.catalogId || this.ids;
      this.$modal.confirm('是否确认删除目录管理编号为"' + catalogIds + '"的数据项？').then(function() {
        return delCatalog(catalogIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/catalog/export', {
        ...this.queryParams
      }, `catalog_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "目录导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('system/catalog/importTemplate', {
      }, `catalog_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
      // 点击确定后关闭弹窗
      this.upload.open = false;
    }
  }
};
</script>
